/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rpt.bd.cenario;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 *
 * @author Roberto
 */
public class CCenarioBd {

    private CGlobal fGlobal = null;
    private CConectorBd fConectorBd = null;
    protected CPersistenciaBd fPersistenciaBd = null;
    protected CEstruturaRegistroBd fEstruturaRegistroBd = null;
    private CGradeBdCenario fGrade = null;
    protected JPanel fDialogoBd = null;
    //CDialogoSelecionarBd fDialogoSelecionarBd = null;
    protected CDialogoGradeBdFormBd fDialogoGradeBdFormBd = null;
    private String fTitulo = null;
    private Object fPainelTabulador = null;
    private CPersistenciaBd fPersistenciaBdMestre = null;
    private int fTipoDialogoBd = 0;
    private String[] fNomesCampoMestre = null;
    private String[] fNomesCampoDetalhe = null;
    private String[] fComparacoes = null;
    private String[] fNomesCampoMestreHeranca = null;
    private String[] fNomesCampoDetalheHeranca = null;
    private int fValoresIdMestre[] = null;

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public CCenarioBd(
            final String aTitulo,
            final CGlobal aGlobal,
            //      CEstruturaRegistroBdCenario aEstruturaRegistroBd,
            final Object aPainelTabulador,
            final int aTipoDialogoBd,
            final int[] aValoresIdMestre,
            final CPersistenciaBdCenario aPertinenciaBdMestre,
            final String[] aNomesCampoMestre,
            final String[] aNomesCampoDetalhe,
            final String[] aComparacoes,
            final String[] aNomesCampoMestreHeranca,
            final String[] aNomesCampoDetalheHeranca,
            final boolean aPreparar) {
        //
        super();
        //
        fGlobal = aGlobal;
        //
        fConectorBd = fGlobal.getConectorBd();
        //
        fTitulo = aTitulo;
        fPainelTabulador = aPainelTabulador;
        fPersistenciaBdMestre = aPertinenciaBdMestre;
        fNomesCampoMestre = aNomesCampoMestre;
        fNomesCampoDetalhe = aNomesCampoDetalhe;
        fComparacoes = aComparacoes;
        fNomesCampoMestreHeranca = aNomesCampoMestreHeranca;
        fNomesCampoDetalheHeranca = aNomesCampoDetalheHeranca;
        fTipoDialogoBd = aTipoDialogoBd;
        fValoresIdMestre = aValoresIdMestre;
        //
        fPersistenciaBd = criarPersistenciaBd();
        //
        preparar(aPreparar);
        //
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public CCenarioBd(
            final String aTitulo,
            final CGlobal aGlobal,
            //      final CEstruturaRegistroBd aEstruturaRegistroBd,
            final int aTipoDialogoBd,
            final JTabbedPane aPainelTabulador,
            final boolean aPreparar) {
        //
        this(aTitulo, aGlobal, aPainelTabulador, aTipoDialogoBd, null, null, null, null, null, null, null, aPreparar);
    }
    
    public CDialogoGradeBdFormBd getDialogoGradeBdFormBd() {
        return fDialogoGradeBdFormBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    protected void preparar(
            final boolean aPreparar) {
        if (!aPreparar) {
            return;
        }
        //
        if (fPersistenciaBdMestre == null) {
            if (fNomesCampoDetalhe != null) {
                fPersistenciaBd.setMestre(fValoresIdMestre, fNomesCampoDetalhe);
            }
        } else {
            fPersistenciaBd.setMestre(fPersistenciaBdMestre, fNomesCampoMestre, fNomesCampoDetalhe, fComparacoes, fNomesCampoMestreHeranca, fNomesCampoDetalheHeranca);
            CConjuntoDadosBd cdm = fPersistenciaBdMestre.getConjuntoDadosBd();
            cdm.adicionarConjuntoDadosBdDetalhe(this.getConjuntoDadosBd());
        }
        getDialogoBd(fTipoDialogoBd, fPainelTabulador);
        //
        //fDialogoBd = new CDialogoBdCenarioEdicao(this);
        //fDialogoBd.AdicionarDialogoCentral(fDialogoGradeFormBd);
        //
        if (fPainelTabulador != null) {
            if (fPainelTabulador instanceof JTabbedPane) {
                final JTabbedPane tp = (JTabbedPane) fPainelTabulador;
                tp.addTab(fTitulo, fDialogoBd);
                tp.setSelectedIndex(tp.getTabCount() - 1);
                if (fDialogoBd instanceof CDialogoBdCenario) {
                    CDialogoBdCenario d1 = (CDialogoBdCenario) fDialogoBd;
                    CDialogoGradeBdFormBd d2 = d1.getDialogoGradeBdFormBd();
                    d2.liberarPreparacao();
                }
            } else if (fPainelTabulador instanceof JFrame) {
                final JFrame d = (JFrame) fPainelTabulador;
                d.setContentPane(fDialogoBd);
            }
        }
        //
        if (fGrade != null) {
            fGrade.autoResizeColWidth();
        }
        //
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    protected CEstruturaRegistroBd getEstruturaRegistroBd() {
        return fEstruturaRegistroBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    protected CPersistenciaBd criarPersistenciaBd() {
        return null;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public CPersistenciaBd getPersistenciaBd() {
        return fPersistenciaBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void getDialogoBd(
            final int aTipoDialogoBd, Object aPainelTabulador) {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public CPersistenciaBd getPertinenciaBd() {
        return fPersistenciaBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public CGlobal getGlobal() {
        return fGlobal;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public CConjuntoDadosBd getConjuntoDadosBd() {
        return fPersistenciaBd.getConjuntoDadosBd();
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public CGradeBdCenario getGrade() {
        return fGrade;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public CRegistroBd getRegistroBdAtual() {
        if (fGrade == null) {
            return null;
        } else {
            return fGrade.getRegistroBdAtual();
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public CCampoRegistroBd getCampoBd(String aNomeCampo) {
        final CRegistroBd r = getRegistroBdAtual();
        if (r == null) {
            return null;
        }
        final CCampoRegistroBd c = r.getCampoRegistroBd(aNomeCampo);
        return c;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public Object getValorCampoBd(String aNomeCampo) {
        final CCampoRegistroBd c = getCampoBd(aNomeCampo);
        if (c == null) {
            return null;
        } else {
            return c.getValorCampoBd();
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public String getValorCampoBdStr(String aNomeCampo) {
        final CCampoRegistroBd c = getCampoBd(aNomeCampo);
        if (c == null) {
            return null;
        } else {
            return c.getValorCampoBdStr();
        }

        /*
        final CRegistroBd r = getRegistroBdAtual();
        if (r == null) return null;
        final CCampoRegistroBd c = r.getCampoRegistroBd(aNomeCampo);
        if (c == null) return null;
        return c.getValorCampoBdStr();
         * 
         */
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void setGrade(
            final CGradeBdCenario aGrade) {
        fGrade = aGrade;
    }
    /*
    public void adicionarRegistroBd() {
    Vector vDadosRegistro = new Vector();
    ResultSet vRs = fPersistenciaBd.getResultSetRegistroBd();
    }
     */

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void carregarConjuntoDadosBd() {
        if (fPersistenciaBd != null) {
            fPersistenciaBd.carregarConjuntoDadosBd();
        }
    }
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
}
